<?php

namespace application\controller\cpservice;

use application\controller\cpservice\ServiceController;

class AdministratorController extends ServiceController {

    private $administratorService;

    public function __construct() {
        parent::__construct();
        $this->administratorService = new \application\service\AdministratorService();
    }

    /**
     * Chi tiết admin
     * @mapping(value=cpservice/administrator/changeactive.json)
     */
    public function changeactive() {
        $id = \core\system\HttpRequest::GET("id");
        $admin = $this->administratorService->get($id);
        if ($admin == null) {
            throw new Exception("Không tìm thấy tài khoản admin yêu cầu");
        }
        $resp = $this->administratorService->changeActive($id, !$admin->active);
        $this->view->response($resp);
    }

    /**
     * Cấp quyền
     * @mapping(value=cpservice/administrator/grantrole.json)
     */
    public function grantRole() {
        $form = \core\system\HttpRequest::content("POST", "json", \application\entity\form\GrantRole::class);
        $resp = $this->administratorService->grantRole($form);
        $this->view->response($resp);
    }

    /**
     * Chi tiết quyền theo admin
     * @mapping(value=cpservice/administrator/getrolebyadministratorId.json)
     */
    public function getRoleByAdministratorId() {
        $id = \core\system\HttpRequest::GET("id");
        $resp = $this->administratorService->getRoleByAdministratorId($id);
        $this->view->response(true, "Danh sách quyền", $resp);
    }

}
